package com.tianqu.tms.www.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class TwiceTransferSearchResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(TwiceTransferSearchResultAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TransferRgActionForm inForm = (TransferRgActionForm)form;
		TransferRgInputViewHelper view = new TransferRgInputViewHelper();
		String serial = inForm.getSerial();
		
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		Codes code = Codes.getInstance();
		Member member = Member.getInstance();
		accept = accept.getAcceptInfoBySerial(serial,"");
		if(accept!=null)
		{
			if(accept.getOwnerType() == 1 && accept.getStatus() == 6 && accept.getSourceType() == 1)
			{
				//根据VIN码查询过户表中所有数据
				TransferAcceptedInfo checkVin = accept.findAcceptInfoByVin(accept.getVin());
				if(checkVin!=null)
				{
					if(checkVin.getLicenseAccepTime() == null|| "".equals(checkVin.getLicenseAccepTime())) 
					{
						setErrorsToRequest(request, TmsConstants.IM012,null);
						return mapping.getInputForward();
					}
					String getMonthBefore = DateUtil.getBeforeMonth(-3);
					String licenseAccepTime = DateUtil.getDateByTimeStamp(checkVin.getLicenseAccepTime());
					boolean flag = DateUtil.compareStringTime(licenseAccepTime, getMonthBefore);// 受理日期比较
					if(flag == false)
					{
						//距离上次过户，已经超过2个月
						setErrorsToRequest(request, TmsConstants.IM013,null);
						return mapping.getInputForward();
					}
					if(accept.getMarketType() == 2)
					{
						//直接打提示
						setErrorsToRequest(request, TmsConstants.IM006,null);
						return mapping.getInputForward();	
					}
					//开始调取数据
					view.setSerial(inForm.getSerial());
					view.setStatus(code.getItemValueByItemIdAndItemCode(Codes.ACCEPT_STATUS, accept.getStatus()));//过户状态
					view.setTransferPrice(StringUtil.addDH(String.valueOf(accept.getTransferPrice())));
					
					// 开始往页面添加数据
					if (accept.getMarketType() == 1) {
						Car car = Car.getInstance();
						car = car.findCarBySerial(accept.getBasicCarId());
						if (car != null) {
							view = toViewHelper(car, view);
							view.setMemberName(member.getMemNameByMemId(car.getMemberId()));
							//根据memebrID到member表中查询落户人信息
							member = member.findMemberInfoByMemId(String.valueOf(car.getMemberId()));
							
							if(member!=null){
								if(checkVin.getNewcarOwnerIdCardNo()!=null&&!"".equals(checkVin.getNewcarOwnerIdCardNo())&&member.getSettleNo()!=null&&!"".equals(member.getSettleNo()))
								{
									if(!checkVin.getNewcarOwnerIdCardNo().equals(member.getSettleNo()))
									{
										//无权处理 和上次落户人不同
										setErrorsToRequest(request, TmsConstants.IM006,null);
										return mapping.getInputForward();											
									}
								}else{
									//无权处理 有一个为空
									setErrorsToRequest(request, TmsConstants.IM006,null);
									return mapping.getInputForward();										
								}
							}else{
								//无权处理 落户人不存在
								setErrorsToRequest(request, TmsConstants.IM006,null);
								return mapping.getInputForward();									
							}
							if(member!=null)
							{
								view.setSettleName(member.getSettleName());
								view.setSettleNo(member.getSettleNo());
								view.setSettlePhone(member.getSettlePhone());								
							}else{
								view.setSettleName("");
								view.setSettleNo("");
								view.setSettlePhone("");									
							}
						}
					}
					if(accept.getMarketType() == 2)
					{					
						TransferPersonalCarInfo person = new TransferPersonalCarInfo();
						person = person.getPersonalCarInfoBySerial(accept.getBasicCarId());
						if(person !=null)
						{
							view = toViewHelper1(person, view);
							view.setMemberName("");
							view.setSettleName("");
							view.setSettleNo("");
							view.setSettlePhone("");	
						}
					}
					TransferProcessHistory his = new TransferProcessHistory();
					view.setLcList(his.loadtHistoryBySerial(inForm.getSerial()));//历史记录
				}else{
					setErrorsToRequest(request, TmsConstants.IM012,null);
					return mapping.getInputForward();
				}
			}else{
				setErrorsToRequest(request, TmsConstants.IM006,null);
				return mapping.getInputForward();
			}	
		}else{
			setErrorsToRequest(request, TmsConstants.IM006,null);
			return mapping.getInputForward();			
		}
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private TransferRgInputViewHelper toViewHelper(Car entity,TransferRgInputViewHelper view) throws TmsDaoException {
		
		Codes code = Codes.getInstance();
		view.setLicensePlate(entity.getLicensePlate());
		view.setVin(entity.getVin());
		view.setModelCode(entity.getModelCode());
		view.setVendorCode(entity.getVendorCode());
		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE, entity.getCarUse()));//使用性质 
		view.setSendCardDate(entity.getSendCardDate());
		view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER, entity.getOwnerType()));//车主类型
		view.setCarOwner(entity.getCarOwner());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setOwnerPhone(entity.getOwnerPhone());
		return view;
	}
	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private TransferRgInputViewHelper toViewHelper1(TransferPersonalCarInfo entity,TransferRgInputViewHelper view) throws TmsDaoException {

		Codes code = Codes.getInstance();
		view.setLicensePlate(entity.getLicensePlate());
		view.setVin(entity.getVin());
		view.setModelCode(entity.getModelCode());
		view.setVendorCode(entity.getVendorCode());
		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE, entity.getCarUse()));//使用性质 
		view.setSendCardDate(entity.getSendCardDate());
		view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER, entity.getOwnerType()));//车主类型
		view.setCarOwner(entity.getCarOwner());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setOwnerPhone(entity.getOwnerPhone());
		return view;
	}
}